package com.smartfactory.sail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartfactory.sail.entity.Contract;
import com.smartfactory.sail.entity.CustCareInfo;
import com.smartfactory.sail.entity.CustContacts;
import com.smartfactory.sail.mapper.ContractMapper;
import com.smartfactory.sail.mapper.CustCareInfoMapper;
import com.smartfactory.sail.mapper.CustContactsMapper;
import com.smartfactory.sail.service.ICustContactsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartfactory.sail.utils.PageUtils;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.cust.ListContactsResp;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户联系人 服务实现类
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Service
public class CustContactsServiceImpl extends ServiceImpl<CustContactsMapper, CustContacts> implements ICustContactsService {

    private CustCareInfoMapper custCareInfoMapper;
    private ContractMapper contractMapper;
    private CustContactsMapper custContactsMapper;

    public CustContactsServiceImpl(CustCareInfoMapper custCareInfoMapper, ContractMapper contractMapper, CustContactsMapper custContactsMapper) {
        this.custCareInfoMapper = custCareInfoMapper;
        this.contractMapper = contractMapper;
        this.custContactsMapper = custContactsMapper;
    }

    @Override
    public Map<String, Object> getStatistics(Long custId) {
        QueryWrapper<CustContacts> custContactsQueryWrapper = new QueryWrapper<>();
        custContactsQueryWrapper.eq("custId", custId);
        custContactsQueryWrapper.select("id");
        List<CustContacts> custContacts1 = custContactsMapper.selectList(custContactsQueryWrapper);
        List<Long> ids = new ArrayList<>();
        for (CustContacts custContacts : custContacts1) {
            ids.add(custContacts.getId());
        }

        QueryWrapper<CustCareInfo> custCareInfoQueryWrapper = new QueryWrapper<>();
        custCareInfoQueryWrapper.in("contactsId", ids);
        //关怀总费用
        Double total = custCareInfoMapper.getCarePriceByQueryParam(custCareInfoQueryWrapper);

        //今年关怀费用
        custCareInfoQueryWrapper.apply("YEAR(  createTime ) = YEAR( NOW( ) )");
        Double nowYear = custCareInfoMapper.getCarePriceByQueryParam(custCareInfoQueryWrapper);
        //总签单额
        QueryWrapper<Contract> contractQueryWrapper = new QueryWrapper<>();
        contractQueryWrapper.eq("custId", custId);
        Double contractTotalPrice = contractMapper.getCarePriceByQueryParam(contractQueryWrapper);
        //总关怀收益比
        double v = contractTotalPrice / total;
        DecimalFormat df = new DecimalFormat("0.00%");
        String r = df.format(v);
        Map<String, Object> result = new HashMap<>();
        result.put("ghzxf", total);
        result.put("jnghxf", nowYear);
        result.put("zqde", contractTotalPrice);
        result.put("zghsyb", r);

        return result;
    }

    @Override
    public IPage<ListContactsResp> getCustContactsListPage(Map<String, Object> queryParam, PageParam pageParam) {
        //分页信息
        Page<ListContactsResp> page = new Page<>();
        PageUtils.createPage(page, pageParam);

        QueryWrapper<ListContactsResp> query = new QueryWrapper<>();
        query.allEq(queryParam);
        return custContactsMapper.getCustContactsListPage(page,query);
    }

}
